package hate.controller;


import hate.json.ResUtil;
import hate.model.Att;
import hate.model.Pro;
import hate.model.SubPro;
import io.ebean.Ebean;
import io.ebean.EbeanServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 子产品管理
 */
@Controller
@RestController
@EnableAutoConfiguration
@RequestMapping("/subPro")
public class SubProducController {

    @Autowired
    private EbeanServer ebeanServer;
    /**
     * 新增产品
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addSubPro" ,method = RequestMethod.POST)
    public ResUtil addSubPro(HttpServletRequest request){

        ResUtil res=new ResUtil();
        String type =request.getParameter("type");
        String sort =request.getParameter("sort");

        SubPro pro=new SubPro();
        pro.setTypeString(type);
        pro.setSort(sort);

        Ebean.save(pro);
        res.setCode("success");

        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/queSubPro" ,method = RequestMethod.POST)
    public ResUtil queSubPro(HttpServletRequest request){
        ResUtil res=new ResUtil();
        List<SubPro> pros=ebeanServer.find(SubPro.class).findList();
        res.setCode("success");
        res.setData(pros);
        return res;
    }

    /**
     * 删除子产品
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delSubPro" ,method = RequestMethod.POST)
    public ResUtil delSubPro(HttpServletRequest request){
        ResUtil res=new ResUtil();
        String userId =request.getParameter("userId");
        String typeId =request.getParameter("typeId");
        SubPro subpro=ebeanServer.find(SubPro.class).where().eq("productId",typeId).findOne();
        ebeanServer.delete(subpro);
        res.setCode("success");
        return res;
    }
    //根据文件id获取文件目录
    private String getFilePathById(String id){
       return  ebeanServer.find(Att.class,id).getPath();
    }

}
